package com.xu.cloud.common.openapi;

import com.xu.cloud.common.conditional.ExcludeProfile;
import com.xu.cloud.common.constant.EnvConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * swagger ui config
 *
 * @author xuguan
 * @since 2024/2/4
 */
@ExcludeProfile(EnvConstant.PROD)
public class OpenApi3UiWebMvcConfig implements WebMvcConfigurer {

    @Value("${springfox.documentation.swagger-ui.base-url:}")
    private String baseUrl;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.
                addResourceHandler(baseUrl + "/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(baseUrl + "/swagger-ui/")
                .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
    }
}
